package Persistencia;

import java.sql.Connection;
import java.sql.PreparedStatement;

import DAO.DAOAutomovil;
import Modelo.Modelo;



public class AdministracionPersistenciaModelo{
	
	private static AdministracionPersistenciaModelo instance;
	
	private AdministracionPersistenciaModelo(){
		
	}
	 
	public static AdministracionPersistenciaModelo getInstance(){
		if(instance==null)
			instance = new AdministracionPersistenciaModelo();
		return instance;
	}
	

	public void insert(Modelo modelo){
		Connection con = PoolConnection.getPoolConnection().getConnection();
		try{
			PreparedStatement ps = con.prepareStatement("insert into Modelos values (?,?,?,?,?,?,?)");
			ps.setString(1, modelo.getMarca());
			ps.setString(2, modelo.getModelo());
			ps.setString(3, modelo.getMotor());
			ps.setString (4, modelo.getDatosSeguridad());
			ps.setString(5, modelo.getModeloTipo().toString());
			ps.setFloat (6,modelo.getCostoAlquilerDia());
			ps.setFloat(7, modelo.getCostoKmExcedente());
			ps.execute();
			PoolConnection.getPoolConnection().closeConnections();
		}catch(Exception e){
			System.out.println("Error al insertar Modelo "+e.getMessage());
		}
	}

	public void update(Modelo modelo){
		Connection con = PoolConnection.getPoolConnection().getConnection();
		try{
			PreparedStatement ps = con.prepareStatement("" +
					"update Modelos set datosSeguridad = ?, modeloTipo = ?, costoAlquilerDia = ?, costoKmExedente =?" +
					"where marca = ? and modelo = ? and motor = ?");
			ps.setString (1, modelo.getDatosSeguridad());
			ps.setString(2, modelo.getModeloTipo().toString());
			ps.setFloat (3,modelo.getCostoAlquilerDia());
			ps.setFloat(4, modelo.getCostoKmExcedente());
			ps.setString(5, modelo.getMarca());
			ps.setString(6, modelo.getModelo());
			ps.setString(7, modelo.getMotor());
			ps.execute();
			PoolConnection.getPoolConnection().closeConnections();
		}catch(Exception e){
			System.out.println("Error al modificar Modelo"+e.getMessage());
		}
	}

	public void delete(Modelo modelo){
		Connection con = PoolConnection.getPoolConnection().getConnection();
		try{
						
			PreparedStatement ps2 = con.prepareStatement("delete from Modelos where where marca = ? and modelo = ? and motor = ?");
			ps2.setString(1, modelo.getMarca());
			ps2.setString(2, modelo.getModelo());
			ps2.setString(3, modelo.getMotor());
			ps2.execute();
			
			PoolConnection.getPoolConnection().closeConnections();
		}catch(Exception e){
			System.out.println("Error al borrar Modelo "+e.getMessage());
		}
	}

}
	